<?php
/**
 * Created by PhpStorm.
 * User: liang
 * Email:1050187159@qq.com
 * Date: 2018/8/16
 * Time: 23:37
 */

namespace Admin\Model;

use Think\Model;

class EventsModel extends Model
{
   protected $trueTableName = 'events';

   public function developsAdd()
   {
       $title = I('post.title');
       if (empty($title)) {
           $this->error = '事件概述不能为空';
           return false;
       }

       if (mb_strlen($title) > 50) {
           $this->error = '事件概述不能超过50个字';
       }

       if (empty(I('post.time'))) {
           $this->error = '时间不能为空';
           return false;
       }
       if (date('Y-m-d', strtotime(I('post.time'))) == I('post.time')) {
           $time = strtotime(I('post.time'));
       } else {
           $this->error = '时间格式不对';
           return false;
       }

       $sort = (int)I('post.sort');

       if (empty($sort)) {
           $this->error = '排序不能为空';
           return false;
       }
       if (!is_integer($sort)) {
           $this->error = '排序必须为整数';
           return false;
       }

       $this->title = $title;
       $this->time = $time;
       $this->sort =$sort;
       if ($this->add()) {
           return true;
       } else {
           return false;
       }

   }

   public function developsModify()
   {
       $id = I('post.id');
       $title = I('post.title');
       if (empty($title)) {
           $this->error = '事件概述不能为空';
           return false;
       }

       if (mb_strlen($title) > 50) {
           $this->error = '事件概述不能超过50个字';
       }

       if (empty(I('post.time'))) {
           $this->error = '时间不能为空';
           return false;
       }
       if (date('Y-m-d', strtotime(I('post.time'))) == I('post.time')) {
           $time = strtotime(I('post.time'));
       } else {
           $this->error = '时间格式不对';
           return false;
       }

       $sort = (int)I('post.sort');

       if (empty($sort)) {
           $this->error = '排序不能为空';
           return false;
       }
       if (!is_integer($sort)) {
           $this->error = '排序必须为整数';
           return false;
       }

       $this->title = $title;
       $this->time = $time;
       $this->sort =$sort;
       if ($this->where(array('id'=>array('eq',$id)))->save() !== false) {
           return true;
       } else {
           return false;
       }
   }
}